<template>
  <h2>readonly和shallowReadonly</h2>
  <h2>{{ state2.car.name }}</h2>
  <button @click="update">更新数据</button>
</template>

<script lang="ts">
import { defineComponent, reactive, readonly, shallowReadonly, ref } from 'vue'

export default defineComponent({
  name: 'App',
  setup() {
    const state = reactive({
      name: '鸣人',
      age: 20,
      car: {
        name: '奔驰',
        color: '红色'
      }
    })
    const states = ref({
      name: '鸣人',
      age: 20,
      car: {
        name: '奔驰',
        color: '红色'
      }
    })
    // 只读的数据 --- 深度的只读
    const state2 = readonly(states)
    // 只读的数据 --- 浅只读
    const state3 = shallowReadonly(state)
    const update = () => {
      // state2.name = '哈哈' //  报错
      // state2.car.name = '哈哈' // 报错
      // state3.name = '哈哈' // 报错
      // state3.car.name = '哈哈'
    }
    return {
      state2,
      update,
      state3
    }
  }
})
</script>

<style scoped>
</style>